package com.syq.play.adapter;

import java.util.List;

import com.syq.play.AppContent;
import com.syq.play.R;
import com.syq.play.model.MediaModel;
import com.syq.play.util.StringUtil;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MediaListAdapter extends BaseAdapter{
	public static final int TYPE_PLAY_TIME=1;
	public static final int TYPE_PLAY_COUNT=2;
	private List<MediaModel> listMedia;
	private LayoutInflater lInflater;
	private Context context;
	private int type=0;
	public MediaListAdapter(Context context, List<MediaModel> listMedia){
		lInflater=LayoutInflater.from(context);
		this.context=context;
		this.listMedia=listMedia;
	}
	public MediaListAdapter(Context context, List<MediaModel> listMedia, int type){
		lInflater=LayoutInflater.from(context);
		this.context=context;
		this.listMedia=listMedia;
		this.type=type;
	}
	public int getCount() {
		return listMedia.size();
	}
	class ViewHolder{
		TextView tvTitle, tvArtist, tvTime;
		ImageView ivMore;
		RelativeLayout rlBg;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		View v=convertView;
		ViewHolder vh=null;
		if(v==null || v.getTag()==null){
			v=lInflater.inflate(R.layout.listview_item, null);
			vh=new ViewHolder();
			vh.tvTitle=(TextView)v.findViewById(R.listview_id.tv_title);
			vh.tvArtist=(TextView)v.findViewById(R.listview_id.tv_artist);
			vh.tvTime=(TextView)v.findViewById(R.listview_id.tv_time);
			vh.ivMore=(ImageView)v.findViewById(R.listview_id.iv_more);
			vh.rlBg=(RelativeLayout)v.findViewById(R.listview_id.rl_bg);
			v.setTag(vh);
		}else{
			vh=(ViewHolder) v.getTag();
		}
		MediaModel item=listMedia.get(position);
		if(item.getMediaTitle()!=null){
			vh.tvTitle.setText("曲目："+item.getMediaTitle());
		}else{
			vh.tvTitle.setText("曲目：未知");
		}
		switch(type){
		case TYPE_PLAY_COUNT:
			if(item.getPlayCount()>0)
				vh.tvArtist.setText("次数："+item.getPlayCount());
			break;
		case TYPE_PLAY_TIME:
			if(item.getPlayTime()>-1)
				vh.tvArtist.setText("时间："+StringUtil.getDateFromTimeInMillis(item.getPlayTime()));
			break;
		default:
			if(item.getMediaArtist()!=null){
				vh.tvArtist.setText("艺术家："+item.getMediaArtist());
			}else{
				vh.tvArtist.setText("艺术家：未知");
			}
			break;
		}
		if(item.getMediaId()==AppContent.playId){
			vh.rlBg.setBackgroundResource(R.color.blue_dark);
		}else{
			vh.rlBg.setBackgroundResource(R.drawable.selector_gridview_item);
		}
		vh.tvTime.setText(""+item.getMediaDuration());
		vh.ivMore.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Toast.makeText(context, "更多", Toast.LENGTH_LONG).show();
			}
		});
		return v;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}
	public List<MediaModel> getListMedia(){
		return listMedia;
	}

}
